package com.intgrt.manager.shiro;


import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.intgrt.manager.entity.SysUser;
import com.intgrt.manager.service.SysUserService;

public class ShiroRealm extends AuthorizingRealm{
	
	@Resource
	private SysUserService service;
	
	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		return info;
	}
	/**
	 * 登录认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationtoken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authenticationtoken;
		SysUser dbUser = service.getNameUser(token.getUsername());
		if (dbUser == null){
			throw new CredentialsException();
		}    
		SimpleAuthenticationInfo authinfo = new SimpleAuthenticationInfo(dbUser, dbUser.getAccpsw(), getName());
		return authinfo;
	}
	
}
